//
//  LBKeshiDetailVC.m
//  V-Learn
//
//  Created by Apple on 2019/1/3.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "LBKeshiDetailVC.h"
#import "XHWebImageAutoSize.h"
#import "LBKeshiCell.h"
#import "LBKeshiModel.h"
#import <UIImageView+WebCache.h>

@interface LBKeshiDetailVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UILabel *priceLab;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableDictionary *rowHeightDict;

@end
static NSString *const cellId = @"KeshiCell";
@implementation LBKeshiDetailVC
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LBNAVBAR_HEIGHT, ScreenW, ScreenH-LBNAVBAR_HEIGHT) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupUI];
    [self requestData];
}
- (void)requestData{
    NSMutableDictionary *data =  [NSMutableDictionary dictionary];
    NSString *token = [UserModel getUserModel].token;
    [data setObject:token forKey:@"token"];
    [data setObject:self.coureseType forKey:@"type"];
    [data setObject:self.courseId forKey:@"refId"];
    
    [WYHXNetWorkManager get:WKKeshiDetail_WKURL params:data completionHandler:^(id responseObj, NSError *error) {
        NSString *code  = responseObj[@"code"];
        if ([code integerValue]==200) {
            NSArray *arr = [LBKeshiModel mj_objectArrayWithKeyValuesArray:responseObj[@"data"]];
//            NSLog(@"%@",arr);
            //            [self.dataArray addObjectsFromArray:arr];
            for (int i=0; i<arr.count; i++) {
                LBKeshiModel *model = arr[i];
                NSString *url = [NSString stringWithFormat:@"%@%@",Public_URL,model.imageUrl];
                
                [self.dataArray addObject:url];
                //
            }
            
        }
        [self.tableView reloadData];
    }];
}
- (void)setupUI{
    self.navigationItem.title = @"课时详情";
    [self.view addSubview:self.tableView];
    
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.tableView.tableFooterView = [UIView new];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    LBKeshiCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if(!cell){
        cell = [[LBKeshiCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    NSString *url = self.dataArray[indexPath.section];
    [cell.arrowImageV sd_setImageWithURL:[NSURL URLWithString:url] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        /** 缓存image size */
        [XHWebImageAutoSize storeImageSize:image forURL:imageURL completed:^(BOOL result) {
            /** reload  */
            if(result)  [tableView  xh_reloadDataForURL:imageURL];
        }];
    }];
    
    return cell;
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *url = self.dataArray[indexPath.section];
    
    return [XHWebImageAutoSize imageHeightForURL:[NSURL URLWithString:url] layoutWidth:[UIScreen mainScreen].bounds.size.width-30 estimateHeight:200];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 10;
    }
    return 5;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [UIView new];
    headerView.backgroundColor = [UIColor whiteColor];
    return headerView;
}


@end
